package com.example.lotteryticketdev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lotteryticketdev.dao.UserDao;
import com.example.lotteryticketdev.entity.User;
import com.example.lotteryticketdev.result.Result;
import com.example.lotteryticketdev.service.UserService;
import com.example.lotteryticketdev.tool.JwtHelper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2023-09-26 22:28:32
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Override
    public Result login(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("password",user.getPassword());
        User one = this.baseMapper.selectOne(wrapper);
        if (one == null){
           return Result.fail();
        }
        String token = JwtHelper.createToken(user.getId(), one.getUsername());
        Map resultMap = new HashMap();
        resultMap.put("token",token);
        return Result.ok(resultMap);
    }
}

